package com.trkj.usercenter.feedback;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.trkj.base.BaseActivity;
import com.trkj.base.Constants;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.User;
import com.trkj.base.newwork.ProgressBarPostDataFromRemote;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;

/**
 * Created by LiZanhong on 2015/06/25.
 */
public class FeedbackAddActivity extends BaseActivity{
    public static final String ACTION = "com.trkj.usercenter.feedback.FeedbackAddActivity";

    @ViewInject(R.id.content)
    private EditText content;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback_add);
        ViewUtils.inject(this);
    }

    public void addFeedBack(View view) {
        String strContent = content.getText().toString();
        if(TextUtils.isEmpty(strContent)){
            toast("请输入反馈内容");
            return;
        }
        
        ProgressBarPostDataFromRemote dataFromRemote =
                new ProgressBarPostDataFromRemote(this);
        dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
            @Override
            public void onResponseResult(String result) {
                boolean success = JSON.parseObject(result).getBoolean("success");
                if(success){
                    toast("反馈提交成功");
                }else{
                    toast("反馈提交失败，请检查网络。");
                }
                Intent status = new Intent();
                status.putExtra("success", success);
                setResult(Activity.RESULT_OK, status);
                finish();
            }
        });

        User user = UserService.getInstance(this).parseUser();
        if (user != null){
            String url = MessageFormat.format(Constants.ApiUrl.FEEDBACK, user.userId);
            
            Log.d("反馈内容", strContent);
            dataFromRemote.execute(url, "content=" + strContent);
        }else{
            toast("反馈提交失败，可能是用户未登陆。");
        }
    }

    @Override
    protected String getCustomerTitle() {
        return "提交反馈";
    }
}
